#############################################################################
# Replication Code for Appendix to 'Bridging Spatial and Saliency Theory'   #
#                                                                           #
# Chitralekha Basu, 2018                                                    #
#############################################################################

rm(list=ls())

sink("appendix_analyses.txt")

data <- read.csv("data_bsas.csv") 

library(lfe)
library(survival)
library(boot)
library(ggplot2)
library(MASS)
library(lmtest)
library(parallel)

########################################################
# FUNCTIONS                                            #
########################################################

# function to generate clustered covariance matrix 
vcovCL <- function(object, cluster = NULL, adjust = NULL)
{
  stopifnot(require("sandwich"))
  
  
  ## cluster specification
  if(is.null(cluster)) cluster <- attr(object, "cluster")
  if(is.null(cluster)) stop("no 'cluster' specification found")
  cluster <- factor(cluster)
  
  
  ## estimating functions and dimensions
  ef <- estfun(object)
  n <- NROW(ef)
  k <- NCOL(ef)
  if(n != length(cluster))
    stop("length of 'cluster' does not match number of observations")
  m <- length(levels(cluster))
  
  
  ## aggregate estimating functions by cluster and compute meat
  ef <- sapply(levels(cluster), function(i) colSums(ef[cluster == i, ,
                                                       drop = FALSE]))
  ef <- if(NCOL(ef) > 1L) t(ef) else matrix(ef, ncol = 1L)
  mt <- crossprod(ef)/n
  
  
  ## bread
  br <- try(bread(object), silent = TRUE)
  if(inherits(br, "try-error")) br <- vcov(object) * n
  
  
  ## put together sandwich
  vc <- 1/n * (br %*% mt %*% br)
  
  
  ## adjustment
  if(is.null(adjust)) adjust <- class(object)[1L] == "lm"
  adj <- if(adjust) m/(m - 1L) * (n - 1L)/(n - k) else m/(m - 1L)
  
  
  ## return
  return(adj * vc)
}

# function organizes multiple plots in single display 
multiplot <- function(..., plotlist=NULL, file, cols=1, layout=NULL) {
  library(grid)
  
  # Make a list from the ... arguments and plotlist
  plots <- c(list(...), plotlist)
  
  numPlots = length(plots)
  
  # If layout is NULL, then use 'cols' to determine layout
  if (is.null(layout)) {
    # Make the panel
    # ncol: Number of columns of plots
    # nrow: Number of rows needed, calculated from # of cols
    layout <- matrix(seq(1, cols * ceiling(numPlots/cols)),
                     ncol = cols, nrow = ceiling(numPlots/cols))
  }
  
  if (numPlots==1) {
    print(plots[[1]])
    
  } else {
    # Set up the page
    grid.newpage()
    pushViewport(viewport(layout = grid.layout(nrow(layout), ncol(layout))))
    
    # Make each plot, in the correct location
    for (i in 1:numPlots) {
      # Get the i,j matrix positions of the regions that contain this subplot
      matchidx <- as.data.frame(which(layout == i, arr.ind = TRUE))
      
      print(plots[[i]], vp = viewport(layout.pos.row = matchidx$row,
                                      layout.pos.col = matchidx$col))
    }
  }
}


########################################################
# CREATING ADDITIONAL VARIABLES FOR ANALYSES           #
########################################################  

# coding parties as mst/niche by approaches (1)-(5)

# Meguid (2005, 2008) #
data$niche.meg <- ifelse(data$ptyfam.cmp=="ethnic"
                         | data$ptyfam.cmp=="nationalist"
                         | data$ptyfam.cmp=="green"
                         | data$ptyfam.cmp=="protest",
                         1,0)
data$mst.meg <- ifelse(data$niche.meg==1,0,1)

data$majmst.meg <- ifelse(data$major==1 & data$mst.meg==1,1,0)
data$majnic.meg <- ifelse(data$major==1 & data$niche.meg==1,1,0)
data$minmst.meg <- ifelse(data$minor==1 & data$mst.meg==1,1,0)
data$minnic.meg <- ifelse(data$minor==1 & data$niche.meg==1,1,0)

# Adams et al. (2006) #
data$niche.adm <- ifelse(data$ptyfam.cmp=="communist"
                         | data$ptyfam.cmp=="nationalist"
                         | data$ptyfam.cmp=="green"
                         | data$ptyfam.cmp=="protest",
                         1,0)

data$mst.adm <- ifelse(data$niche.adm==1,0,1)

data$majmst.adm <- ifelse(data$major==1 & data$mst.adm==1,1,0)
data$majnic.adm <- ifelse(data$major==1 & data$niche.adm==1,1,0)
data$minmst.adm <- ifelse(data$minor==1 & data$mst.adm==1,1,0)
data$minnic.adm <- ifelse(data$minor==1 & data$niche.adm==1,1,0)

# Meguid (2005, 2008) and Adams et al. (2006) #
data$niche.flex <- ifelse(data$ptyfam.cmp=="communist"
                          | data$ptyfam.cmp=="nationalist"
                          | data$ptyfam.cmp=="green"
                          | data$ptyfam.cmp=="ethnic"
                          | data$ptyfam.cmp=="protest",
                          1,0)

data$mst.flex <- ifelse(data$niche.flex==1,0,1)

data$majmst.flex <- ifelse(data$major==1 & data$mst.flex==1,1,0)
data$majnic.flex <- ifelse(data$major==1 & data$niche.flex==1,1,0)
data$minmst.flex <- ifelse(data$minor==1 & data$mst.flex==1,1,0)
data$minnic.flex <- ifelse(data$minor==1 & data$niche.flex==1,1,0)

# Bischof (2017) #
data$bischofn <- ifelse(data$bischofn2 > median(data$bischofn2,na.rm=T),1,0)
data$bischofmst <- ifelse(data$bischofn2 <= median(data$bischofn2,na.rm=T),1,0)

data$majmst.bischof <- ifelse(data$major==1 & data$bischofmst==1,1,0)
data$majnic.bischof <- ifelse(data$major==1 & data$bischofn==1,1,0)
data$minmst.bischof <- ifelse(data$minor==1 & data$bischofmst==1,1,0)
data$minnic.bischof <- ifelse(data$minor==1 & data$bischofn==1,1,0)

# Meyer and Miller (2015) #
data$meyerniche <- ifelse(data$meyern > median(data$meyern,na.rm=T),1,0)
data$meyermst <- ifelse(data$meyern <= median(data$meyern,na.rm=T),1,0)

data$majmst.meyer <- ifelse(data$major==1 & data$meyermst==1,1,0)
data$majnic.meyer <- ifelse(data$major==1 & data$meyerniche==1,1,0)
data$minmst.meyer <- ifelse(data$minor==1 & data$meyermst==1,1,0)
data$minnic.meyer <- ifelse(data$minor==1 & data$meyerniche==1,1,0)


########################################################
#       PREPARING DATASET FOR BUC ESTIMATION           #
########################################################

data1 <- data
data2 <- data
data3 <- data

data1$bucsal_ches <- ifelse(data1$salience_ches>=3,1,0)

data2$bucsal_ches <- ifelse(data2$salience_ches>=2,1,0)

data3$bucsal_ches <- ifelse(data3$salience_ches>=1,1,0)

bucpooled <- rbind(data1,data2,data3)


########################################################
# APPENDIX A: LIST OF PARTIES (TABLE A.1)              #
########################################################  

majmst <- unique(data$chespty[data$mst.flex==1 & data$major==1])
majmst

majnic <- unique(data$chespty[data$niche.flex==1 & data$major==1])
majnic

minmst <- unique(data$chespty[data$mst.flex==1 & data$minor==1])
minmst

minnic <- unique(data$chespty[data$niche.flex==1 & data$minor==1])
minnic


#######################################################################
# APPENDIX B: DESCRIPTIVE STATISTICS (TABLE B.1)                      #
#######################################################################

# descriptive statistics for full sample

summary(data$salience_ches) # party emphasis
summary(data$supporterext)  # supporter extremism
summary(data$supporterdist) # supporter distinctiveness

# descriptive statistics for major parties 

summary(data[data$major==1,]$salience_ches) # party emphasis
summary(data[data$major==1,]$supporterext)  # supporter extremism
summary(data[data$major==1,]$supporterdist) # supporter distinctiveness

# descriptive statistics for minor parties 

summary(data[data$minor==1,]$salience_ches) # party emphasis
summary(data[data$minor==1,]$supporterext)  # supporter extremism
summary(data[data$minor==1,]$supporterdist) # supporter distinctiveness


#######################################################################
# APPENDIX B: DENSITY PLOTS OF EUROPEAN PREFERENCES (FIGURE B.1)      #
#######################################################################

EES14 <- read.csv("EES2014-cleaned.csv")

dev.new()

pdf("distplots.pdf", width=14, height=10)

par(mfrow=c(2,4))

plot(density(EES14$state.intervention,bw=1,na.rm=T),
     main="State Intervention",ylim=c(0,0.2))

plot(density(EES14$civil.liberties,bw=1,na.rm=T),
     main="Civil Liberties",ylim=c(0,0.2))

plot(density(EES14$environment,bw=1,na.rm=T),
     main="Environment",ylim=c(0,0.2))

plot(density(EES14$EU.integration,bw=1,na.rm=T),
     main="EU Integration",ylim=c(0,0.2))

plot(density(EES14$immigration,bw=1,na.rm=T),
     main="Immigration",ylim=c(0,0.2))

plot(density(EES14$public.services.vs.taxes,bw=1,na.rm=T),
     "Taxes vs. Spending",ylim=c(0,0.2))

plot(density(EES14$redistribution,bw=1,na.rm=T),
     main="Redistribution",ylim=c(0,0.2))

plot(density(EES14$social.lifestyle,bw=1,na.rm=T),
     main="Same-Sex Marriage",ylim=c(0,0.2))

dev.off()


########################################################
# APPENDIX D: TABLE D.1                                #
########################################################

# Model 2 (Past)
clog2 <- clogit(bucsal_ches ~ supporterext:major
                + supporterext
                + supportersd
                + govt_allyrs
                + major:envt       
                + major:EU           
                + major:immig           
                + major:spendvtax  
                + major:redistribution          
                + major:ssm        
                + major:stint
                + major:civlib
                + partner_emph_ches
                + strata(country.issue)
                + cluster(country.issue),
                data=bucpooled,
                method="efron")

summary(clog2)
nrow(model.frame(clog2))/3  
logLik(clog2) 

# coding party major/minor status using 2014 seat share 
bucpooled$cmaj <- ifelse(bucpooled$seats14>=13,1,0)
bucpooled$cmin <- ifelse(bucpooled$seats14<13,1,0)

# Model 2 (Current)
clog2c <- clogit(bucsal_ches ~ supporterext:cmaj
                 + supporterext
                 + supportersd
                 + govt_allyrs
                 + cmaj:envt       
                 + cmaj:EU           
                 + cmaj:immig           
                 + cmaj:spendvtax  
                 + cmaj:redistribution          
                 + cmaj:ssm        
                 + cmaj:stint
                 + cmaj:civlib
                 + partner_emph_ches
                 + strata(country.issue)
                 + cluster(country.issue),
                 data=bucpooled,
                 method="efron")

summary(clog2c)
nrow(model.frame(clog2c))/3  
logLik(clog2c) 

# Model 4 (Past)
clog4 <- clogit(bucsal_ches ~ supporterdist:major
                + supporterdist
                + supportersd
                + govt_allyrs
                + major:envt       
                + major:EU           
                + major:immig           
                + major:spendvtax  
                + major:redistribution          
                + major:ssm        
                + major:stint
                + major:civlib
                + partner_emph_ches
                + strata(country.issue)
                + cluster(country.issue),
                data=bucpooled,
                method="efron")

summary(clog4)
nrow(model.frame(clog4))/3  
logLik(clog4) 

# Model 4 (Current)
clog4c <- clogit(bucsal_ches ~ supporterdist:cmaj
                 + supporterdist
                 + supportersd
                 + govt_allyrs
                 + cmaj:envt       
                 + cmaj:EU           
                 + cmaj:immig           
                 + cmaj:spendvtax  
                 + cmaj:redistribution          
                 + cmaj:ssm        
                 + cmaj:stint
                 + cmaj:civlib
                 + partner_emph_ches
                 + strata(country.issue)
                 + cluster(country.issue),
                 data=bucpooled,
                 method="efron")

summary(clog4c)
nrow(model.frame(clog4c))/3  
logLik(clog4c) 


########################################################
# APPENDIX D: TABLE D.2                                #
########################################################

# (1) Original
clog2 <- clogit(bucsal_ches ~ supporterext:major
                + supporterext
                + supportersd
                + govt_allyrs
                + major:envt       
                + major:EU           
                + major:immig           
                + major:spendvtax  
                + major:redistribution          
                + major:ssm        
                + major:stint
                + major:civlib
                + partner_emph_ches
                + strata(country.issue)
                + cluster(country.issue),
                data=bucpooled,
                method="efron")

summary(clog2)
nrow(model.frame(clog2))/3  
logLik(clog2) 

# coding party major/minor status using 1995 seat share 
bucpooled$hmaj <- ifelse(bucpooled$seats95>=13,1,0)
bucpooled$hmin <- ifelse(bucpooled$seats95<13,1,0)

# (2) Proxy 
clogiv1 <- clogit(bucsal_ches ~ supporterext:hmaj
                  + supporterext
                  + supportersd
                  + govt_allyrs
                  + partner_emph_ches
                  + hmaj:envt       
                  + hmaj:EU           
                  + hmaj:immig           
                  + hmaj:spendvtax  
                  + hmaj:redistribution          
                  + hmaj:ssm        
                  + hmaj:stint
                  + hmaj:civlib
                  + strata(country.issue)
                  + cluster(country.issue),
                  data=bucpooled,
                  method="efron")

summary(clogiv1) 
nrow(model.frame(clogiv1))/3  
logLik(clogiv1) 

# calculating first-stage F statistic
d2 <- subset(data,is.na(data$seats95)==F 
             & is.na(data$major)==F 
             & is.na(data$supportersd)==F
             & is.na(data$govt_allyrs)==F
             & is.na(data$partner_emph_ches)==F)

fstage0 <- lm(major ~ 1 + supportersd
               + govt_allyrs
               + partner_emph_ches, 
               data=d2)

fstage1 <- lm(major ~ seats95 + supportersd
               + govt_allyrs
               + partner_emph_ches, 
               data=d2)

anova(fstage1,fstage0,test="F")

pchisq(deviance(fstage0)-deviance(fstage1),
       df.residual(fstage0)-df.residual(fstage1),
       lower.tail=FALSE)

# extracting fitted values from first stage for 2SPS analysis
d3 <- as.data.frame(cbind(as.character(d2$chespty),
                          as.character(d2$country.issue),
                          as.numeric(fstage1$fitted.values)))

colnames(d3)[1:3] <- c("chespty","country.issue","fvmaj")

bucpoolediv <- merge(bucpooled,d3,all.x=T,all.y=F,by=c("chespty","country.issue"))

bucpoolediv$fvmaj<- as.numeric(as.character(bucpoolediv$fvmaj))
bucpoolediv$fvmin <- 1-bucpoolediv$fvmaj

# (3) 2SPS
sstage <- clogit(bucsal_ches ~ supporterext:fvmaj
                 + supporterext  
                 + supportersd
                 + govt_allyrs
                 + partner_emph_ches
                 + fvmaj:envt       
                 + fvmaj:EU           
                 + fvmaj:immig           
                 + fvmaj:spendvtax  
                 + fvmaj:redistribution          
                 + fvmaj:ssm        
                 + fvmaj:stint
                 + fvmaj:civlib
                 + strata(country.issue)
                 + cluster(country.issue),
                 data=bucpoolediv,
                 method="efron")

summary(sstage)
nrow(model.frame(sstage))/3 
logLik(sstage) 

# bootstrapping standard errors for (3)
cis <-  as.numeric(duplicated(bucpoolediv$country.issue))
cis2 <- as.data.frame(cbind(as.character(bucpoolediv$country.issue),cis))

cis2 <- subset(cis2,cis2$cis==0) # identifying unique country-issue clusters
cis2$cis <- NULL

b.sample = function(data, d) {
  
  data.oe <- data[d,]
  
  data.oe <- as.data.frame(data.oe)
  
  colnames(data.oe)[1] <- "country.issue"
  
  bucsample <- merge(bucpoolediv,data.oe,by="country.issue",all.y=T) 
  
  bucsample$issue <- droplevels(as.factor(bucsample$issue))
  
  results <- clogit(bucsal_ches ~ supporterext:fvmaj
                    + supporterext
                    + supportersd
                    + govt_allyrs
                    + partner_emph_ches
                    + fvmaj:envt       
                    + fvmaj:EU           
                    + fvmaj:immig           
                    + fvmaj:spendvtax  
                    + fvmaj:redistribution          
                    + fvmaj:ssm        
                    + fvmaj:stint
                    + fvmaj:civlib
                    + strata(country.issue)
                    + cluster(country.issue),
                    data=bucsample,
                    method="efron")
  
  coeff = as.numeric(results$coefficients)  
  return(coeff)
  
}

set.seed(4511648)

results = boot(data=cis2, statistic=b.sample, R=1000, 
               parallel = "snow")

results # print bootstrapped SEs for (3)

bucpoolediv$fvmajr <- bucpoolediv$major-(bucpoolediv$fvmaj)

# (4) 2SRI
sstage2 <- clogit(bucsal_ches ~ supporterext:major
                  + supporterext:fvmajr
                  + supporterext
                  + supportersd
                  + govt_allyrs
                  + partner_emph_ches
                  + major:envt       
                  + major:EU           
                  + major:immig           
                  + major:spendvtax  
                  + major:redistribution          
                  + major:ssm        
                  + major:stint
                  + major:civlib
                  + fvmajr:envt       
                  + fvmajr:EU           
                  + fvmajr:immig           
                  + fvmajr:spendvtax  
                  + fvmajr:redistribution          
                  + fvmajr:ssm        
                  + fvmajr:stint
                  + fvmajr:civlib
                  + strata(country.issue)
                  + cluster(country.issue),
                  data=bucpoolediv,
                  method="efron")

summary(sstage2)
nrow(model.frame(sstage2))/3 
logLik(sstage2)

b.sample2 = function(data, d) {
  
  data.oe = data[d,]
  
  data.oe <- as.data.frame(data.oe)
  
  colnames(data.oe)[1] <- "country.issue"
  
  bucsample <- merge(bucpoolediv,data.oe,by="country.issue",all.y=T) 
  
  bucsample$issue <- droplevels(as.factor(bucsample$issue))
  
  results <- clogit(bucsal_ches ~ supporterext:major
                    + supporterext:fvmajr
                    + supporterext
                    + supportersd
                    + govt_allyrs
                    + partner_emph_ches
                    + major:envt       
                    + major:EU           
                    + major:immig           
                    + major:spendvtax  
                    + major:redistribution          
                    + major:ssm        
                    + major:stint
                    + major:civlib
                    + fvmajr:envt       
                    + fvmajr:EU           
                    + fvmajr:immig           
                    + fvmajr:spendvtax  
                    + fvmajr:redistribution          
                    + fvmajr:ssm        
                    + fvmajr:stint
                    + fvmajr:civlib
                    + strata(country.issue)
                    + cluster(country.issue),
                    data=bucsample,
                    method="efron")
  
  coeff = as.numeric(results$coefficients)  
  return(coeff)
  
}

set.seed(4511648)

results2 = boot(data=cis2, statistic=b.sample2, R=1000,
                parallel = "snow")

results2 # print bootstrapped SEs for (4)


########################################################
# APPENDIX D: FIGURE D.1                               #
########################################################

# converting data from party-issue level to party level
data$dup_pty <- ifelse(duplicated(data$chespty)==T,1,0)
pcdata <- subset(data,data$dup_pty==0)

### Figure D.1 subfigure (1) ###
pcdata$type.meg <- ifelse(pcdata$majmst.meg==1,1,
                          ifelse(pcdata$majnic.meg==1,2,
                                 ifelse(pcdata$minmst.meg==1,3,4)))

pcdata_n <- subset(pcdata,is.na(pcdata$type.meg)==F)

numbers <- c(sum(pcdata_n$majmst.meg)/length(pcdata_n$major),
             sum(pcdata_n$majnic.meg,na.rm=T)/length(pcdata_n$major),
             sum(pcdata_n$minmst.meg,na.rm=T)/length(pcdata_n$major),
             sum(pcdata$minnic.meg,na.rm=T)/length(pcdata_n$major))

g <- ggplot(pcdata_n,aes(as.factor(type.meg)))

pb1 <- g + geom_bar(aes(y = (..count..)/sum(..count..)))+ 
  theme_bw() + ggtitle("(1) Meguid (2005, 2008)") +
  xlab("Party Type") +
  ylab("Proportion") +
  ylim(0,1) +
  scale_x_discrete(labels=c("Major Mst","Major Niche","Minor Mst","Minor Niche")) +
  theme(axis.title = element_text(family = "sans", size=15),
        axis.text= element_text(family = "sans", size=10),
        axis.text.x.top = element_text(size=8),
        plot.title = element_text(size = 12),
        legend.text=element_text(size=10))

### Figure D.1 subfigure (2) ###
pcdata$type.adm <- ifelse(pcdata$majmst.adm==1,1,
                          ifelse(pcdata$majnic.adm==1,2,
                                 ifelse(pcdata$minmst.adm==1,3,4)))

pcdata_n <- subset(pcdata,is.na(pcdata$type.adm)==F)

numbers <- c(sum(pcdata_n$majmst.adm)/length(pcdata_n$major),
             sum(pcdata_n$majnic.adm,na.rm=T)/length(pcdata_n$major),
             sum(pcdata_n$minmst.adm,na.rm=T)/length(pcdata_n$major),
             sum(pcdata$minnic.adm,na.rm=T)/length(pcdata_n$major))

g <- ggplot(pcdata_n,aes(as.factor(type.adm)))

pb2 <- g + geom_bar(aes(y = (..count..)/sum(..count..)))+ 
  theme_bw() + ggtitle("(2) Adams (2006)") +
  xlab("Party Type") +
  ylab("Proportion") +
  ylim(0,1) +
  scale_x_discrete(labels=c("Major Mst","Major Niche","Minor Mst","Minor Niche")) +
  theme(axis.title = element_text(family = "sans", size=15),
        axis.text= element_text(family = "sans", size=10),
        axis.text.x.top = element_text(size=8),
        plot.title = element_text(size = 12),
        legend.text=element_text(size=10))

### Figure D.1 subfigure (3) ###
pcdata$type.flex <- ifelse(pcdata$majmst.flex==1,1,
                           ifelse(pcdata$majnic.flex==1,2,
                                  ifelse(pcdata$minmst.flex==1,3,4)))

pcdata_n <- subset(pcdata,is.na(pcdata$type.flex)==F)

numbers <- c(sum(pcdata_n$majmst.flex)/length(pcdata_n$major),
             sum(pcdata_n$majnic.flex,na.rm=T)/length(pcdata_n$major),
             sum(pcdata_n$minmst.flex,na.rm=T)/length(pcdata_n$major),
             sum(pcdata_n$minnic.flex,na.rm=T)/length(pcdata_n$major))

g <- ggplot(pcdata_n,aes(as.factor(type.flex)))

pb3 <- g + geom_bar(aes(y = (..count..)/sum(..count..)))+ 
  theme_bw() + ggtitle("(3) Meguid (2005, 2008) and Adams (2006)") +
  xlab("Party Type") +
  ylab("Proportion") +
  ylim(0,1) +
  scale_x_discrete(labels=c("Major Mst","Major Niche","Minor Mst","Minor Niche")) +
  theme(axis.title = element_text(family = "sans", size=15),
        axis.text= element_text(family = "sans", size=10),
        axis.text.x.top = element_text(size=8),
        plot.title = element_text(size = 12),
        legend.text=element_text(size=10))

### Figure D.1 subfigure (4) ###
pcdata$type.bischof <- ifelse(pcdata$majmst.bischof==1,1,
                              ifelse(pcdata$majnic.bischof==1,2,
                                     ifelse(pcdata$minmst.bischof==1,3,4)))

pcdata_n <- subset(pcdata,is.na(pcdata$type.bischof)==F)

numbers <- c(sum(pcdata_n$majmst.bischof)/length(pcdata_n$major),
             sum(pcdata_n$majnic.bischof,na.rm=T)/length(pcdata_n$major),
             sum(pcdata_n$minmst.bischof,na.rm=T)/length(pcdata_n$major),
             sum(pcdata_n$minnic.bischof,na.rm=T)/length(pcdata_n$major))

g <- ggplot(pcdata_n,aes(as.factor(type.bischof)))

pb4 <- g + geom_bar(aes(y = (..count..)/sum(..count..)))+ 
  theme_bw() + ggtitle("(4) Bischof (2017)") +
  xlab("Party Type") +
  ylab("Proportion") +
  ylim(0,1) +
  scale_x_discrete(labels=c("Major Mst","Major Niche","Minor Mst","Minor Niche")) +
  theme(axis.title = element_text(family = "sans", size=15),
        axis.text= element_text(family = "sans", size=10),
        axis.text.x.top = element_text(size=8),
        plot.title = element_text(size = 12),
        legend.text=element_text(size=10))

### Figure D.1 subfigure (5) ###
pcdata$type.meyer <- ifelse(pcdata$majmst.meyer==1,1,
                            ifelse(pcdata$majnic.meyer==1,2,
                                   ifelse(pcdata$minmst.meyer==1,3,4)))

pcdata_n <- subset(pcdata,is.na(pcdata$type.meyer)==F)

numbers <- c(sum(pcdata_n$majmst.meyer)/length(pcdata_n$major),
             sum(pcdata_n$majnic.meyer,na.rm=T)/length(pcdata_n$major),
             sum(pcdata_n$minmst.meyer,na.rm=T)/length(pcdata_n$major),
             sum(pcdata_n$minnic.meyer,na.rm=T)/length(pcdata_n$major))

g <- ggplot(pcdata_n,aes(as.factor(type.meyer)))

pb5 <- g + geom_bar(aes(y = (..count..)/sum(..count..)))+ 
  theme_bw() + ggtitle("(5) Meyer and Miller (2015)") +
  xlab("Party Type") +
  ylab("Proportion") +
  ylim(0,1) +
  scale_x_discrete(labels=c("Major Mst","Major Niche","Minor Mst","Minor Niche")) +
  theme(axis.title = element_text(family = "sans", size=15),
        axis.text= element_text(family = "sans", size=10),
        axis.text.x.top = element_text(size=8),
        plot.title = element_text(size = 12),
        legend.text=element_text(size=10))

### generating Figure D.1 ###

dev.new()

pdf("partydists.pdf", width=14, height=10)

multiplot(pb1,pb2,pb3,pb4,pb5,cols=3)

dev.off()


########################################################
# APPENDIX D: TABLE D.4                                #
########################################################

# (1) Meguid (2005)
cmst1 <- clogit(bucsal_ches ~ supporterext:niche.meg:major
                + supporterext:niche.meg:minor
                + supporterext:mst.meg:major
                + supporterext
                + supportersd
                + govt_allyrs
                + partner_emph_ches
                + niche.meg:major:envt       
                + niche.meg:major:EU           
                + niche.meg:major:immig           
                + niche.meg:major:spendvtax  
                + niche.meg:major:redistribution          
                + niche.meg:major:ssm        
                + niche.meg:major:stint
                + niche.meg:major:civlib
                + niche.meg:minor:envt       
                + niche.meg:minor:EU           
                + niche.meg:minor:immig           
                + niche.meg:minor:spendvtax  
                + niche.meg:minor:redistribution          
                + niche.meg:minor:ssm        
                + niche.meg:minor:stint
                + niche.meg:minor:civlib               
                + mst.meg:major:envt       
                + mst.meg:major:EU           
                + mst.meg:major:immig           
                + mst.meg:major:spendvtax  
                + mst.meg:major:redistribution          
                + mst.meg:major:ssm        
                + mst.meg:major:stint
                + mst.meg:major:civlib
                + strata(country.issue)
                + cluster(country.issue),
                data=bucpooled,
                method="efron")

summary(cmst1)
nrow(model.frame(cmst1))/3 
logLik(cmst1) 

# (2) Adams (2006)
cmst2 <- clogit(bucsal_ches ~ supporterext:niche.adm:major
                + supporterext:niche.adm:minor
                + supporterext:mst.adm:major
                + supporterext
                + supportersd
                + govt_allyrs
                + partner_emph_ches
                + niche.adm:major:envt       
                + niche.adm:major:EU           
                + niche.adm:major:immig           
                + niche.adm:major:spendvtax  
                + niche.adm:major:redistribution          
                + niche.adm:major:ssm        
                + niche.adm:major:stint
                + niche.adm:major:civlib
                + niche.adm:minor:envt       
                + niche.adm:minor:EU           
                + niche.adm:minor:immig           
                + niche.adm:minor:spendvtax  
                + niche.adm:minor:redistribution          
                + niche.adm:minor:ssm        
                + niche.adm:minor:stint
                + niche.adm:minor:civlib               
                + mst.adm:major:envt       
                + mst.adm:major:EU           
                + mst.adm:major:immig           
                + mst.adm:major:spendvtax  
                + mst.adm:major:redistribution          
                + mst.adm:major:ssm        
                + mst.adm:major:stint
                + mst.adm:major:civlib
                + strata(country.issue)
                + cluster(country.issue),
                data=bucpooled,
                method="efron")

summary(cmst2)
nrow(model.frame(cmst2))/3 
logLik(cmst2) 

# (3) Bischof (2017)
cmst3 <- clogit(bucsal_ches ~ supporterext:bischofn:major
                + supporterext:bischofn:minor
                + supporterext:bischofmst:major
                + supporterext
                + supportersd
                + govt_allyrs
                + partner_emph_ches
                + bischofn:major:envt       
                + bischofn:major:EU           
                + bischofn:major:immig           
                + bischofn:major:spendvtax  
                + bischofn:major:redistribution          
                + bischofn:major:ssm        
                + bischofn:major:stint
                + bischofn:major:civlib
                + bischofn:minor:envt       
                + bischofn:minor:EU           
                + bischofn:minor:immig           
                + bischofn:minor:spendvtax  
                + bischofn:minor:redistribution          
                + bischofn:minor:ssm        
                + bischofn:minor:stint
                + bischofn:minor:civlib               
                + bischofmst:major:envt       
                + bischofmst:major:EU           
                + bischofmst:major:immig           
                + bischofmst:major:spendvtax  
                + bischofmst:major:redistribution          
                + bischofmst:major:ssm        
                + bischofmst:major:stint
                + bischofmst:major:civlib
                + strata(country.issue)
                + cluster(country.issue),
                data=bucpooled,
                method="efron")

summary(cmst3)
nrow(model.frame(cmst3))/3 
logLik(cmst3) 

# (4) Meyer & Miller (2015)
cmst4 <- clogit(bucsal_ches ~ supporterext:meyerniche:major
                + supporterext:meyerniche:minor
                + supporterext:meyermst:major
                + supporterext
                + supportersd
                + govt_allyrs
                + partner_emph_ches
                + meyerniche:major:envt       
                + meyerniche:major:EU           
                + meyerniche:major:immig           
                + meyerniche:major:spendvtax  
                + meyerniche:major:redistribution          
                + meyerniche:major:ssm        
                + meyerniche:major:stint
                + meyerniche:major:civlib
                + meyerniche:minor:envt       
                + meyerniche:minor:EU           
                + meyerniche:minor:immig           
                + meyerniche:minor:spendvtax  
                + meyerniche:minor:redistribution          
                + meyerniche:minor:ssm        
                + meyerniche:minor:stint
                + meyerniche:minor:civlib               
                + meyermst:major:envt       
                + meyermst:major:EU           
                + meyermst:major:immig           
                + meyermst:major:spendvtax  
                + meyermst:major:redistribution          
                + meyermst:major:ssm        
                + meyermst:major:stint
                + meyermst:major:civlib                
                + strata(country.issue)
                + cluster(country.issue),
                data=bucpooled,
                method="efron")

summary(cmst4)
nrow(model.frame(cmst4))/3 
logLik(cmst4) 


########################################################
# APPENDIX E: TABLE E.1                                #
########################################################

# Model 1
cmp1 <- felm(salience_cmp ~ -1 + supporterext:major
             + supporterext
             + major:issue
             | country.issue | 0 | country.issue,
             exactDOF='rM',
             data=data)

summary(cmp1)
nrow(cmp1$fitted.values)

# Model 2
cmp2 <- felm(salience_cmp ~ -1 + supporterext:major
             + supporterext
             + supportersd
             + govt_allyrs
             + partner_emph_cmp
             + major:issue
             | country.issue | 0 | country.issue,
             exactDOF='rM',
             data=data)

summary(cmp2)
nrow(cmp2$fitted.values)

# Model 3
cmp3 <- felm(salience_cmp ~ -1 + supporterdist:major
             + supporterdist
             + major:issue
             | country.issue | 0 | country.issue,
             exactDOF='rM',
             data=data)

summary(cmp3)
nrow(cmp3$fitted.values)

# Model 4
cmp4 <- felm(salience_cmp ~ -1 + supporterdist:major
             + supporterdist
             + supportersd
             + govt_allyrs
             + partner_emph_cmp
             + major:issue
             | country.issue | 0 | country.issue,
             exactDOF='rM',
             data=data)

summary(cmp4)
nrow(cmp4$fitted.values)


########################################################
# APPENDIX E: TABLE E.2                                #
########################################################

# Model 1
clog1c <- clogit(bucsal_ches ~ supporterext:hst_avg00
                 + supporterext
                 + hst_avg00:envt       
                 + hst_avg00:EU           
                 + hst_avg00:immig           
                 + hst_avg00:spendvtax  
                 + hst_avg00:redistribution          
                 + hst_avg00:ssm        
                 + hst_avg00:stint
                 + hst_avg00:civlib
                 + strata(country.issue)
                 + cluster(country.issue),
                 data=bucpooled,
                 method="efron")

summary(clog1c)
nrow(model.frame(clog1c))/3 
logLik(clog1c) 

# Model 2
clog2c <- clogit(bucsal_ches ~ supporterext:hst_avg00
                 + supporterext
                 + supportersd
                 + govt_allyrs
                 + hst_avg00:envt       
                 + hst_avg00:EU           
                 + hst_avg00:immig           
                 + hst_avg00:spendvtax  
                 + hst_avg00:redistribution          
                 + hst_avg00:ssm        
                 + hst_avg00:stint
                 + hst_avg00:civlib
                 + partner_emph_ches
                 + strata(country.issue)
                 + cluster(country.issue),
                 data=bucpooled,
                 method="efron")

summary(clog2c)
nrow(model.frame(clog2c))/3 
logLik(clog2c) 

# Model 3 
clog3c <- clogit(bucsal_ches ~ supporterdist:hst_avg00
                 + supporterdist
                 + hst_avg00:envt       
                 + hst_avg00:EU           
                 + hst_avg00:immig           
                 + hst_avg00:spendvtax  
                 + hst_avg00:redistribution          
                 + hst_avg00:ssm        
                 + hst_avg00:stint
                 + hst_avg00:civlib
                 + strata(country.issue)
                 + cluster(country.issue),
                 data=bucpooled,
                 method="efron")

summary(clog3c)
nrow(model.frame(clog3c))/3 
logLik(clog3c) 

# Model 4
clog4c <- clogit(bucsal_ches ~ supporterdist:hst_avg00
                 + supporterdist
                 + supportersd
                 + govt_allyrs
                 + hst_avg00:envt       
                 + hst_avg00:EU           
                 + hst_avg00:immig           
                 + hst_avg00:spendvtax  
                 + hst_avg00:redistribution          
                 + hst_avg00:ssm        
                 + hst_avg00:stint
                 + hst_avg00:civlib
                 + partner_emph_ches
                 + strata(country.issue)
                 + cluster(country.issue),
                 data=bucpooled,
                 method="efron")

summary(clog4c)
nrow(model.frame(clog4c))/3 
logLik(clog4c) 


########################################################
# APPENDIX E: TABLE E.3                                #
########################################################

options(max.print=1000000)

# Model 1
clog1ol <- polr(as.factor(salience_ches) ~ supporterext:major
                + supporterext
                + major:envt       
                + major:EU           
                + major:immig           
                + major:spendvtax  
                + major:redistribution          
                + major:ssm        
                + major:stint
                + major:civlib
                + country.issue,
                method="logistic",
                data=data,
                Hess=T)

# calculating clustered standard errors
dat <- data[is.na(data$salience_ches)==F & is.na(data$supporterext)==F,]
coeftest(clog1ol, vcov=vcovCL(clog1ol, factor(dat$country.issue)))

nrow(model.frame(clog1ol)) 
logLik(clog1ol)

# Model 2
clog2ol <- polr(as.factor(salience_ches) ~ supporterext:major
                + supporterext
                + supportersd
                + govt_allyrs
                + partner_emph_ches
                + major:envt       
                + major:EU           
                + major:immig           
                + major:spendvtax  
                + major:redistribution          
                + major:ssm        
                + major:stint
                + major:civlib
                + country.issue,
                method="logistic",
                data=data,
                Hess=T)

dat2 <- data[is.na(data$salience_ches)==F 
             & is.na(data$supporterext)==F
             & is.na(data$partner_emph_ches)==F
             & is.na(data$supportersd)==F
             & is.na(data$govt_allyrs)==F,]

coeftest(clog2ol, vcov=vcovCL(clog2ol, factor(dat2$country.issue)))

nrow(model.frame(clog2ol)) 
logLik(clog2ol)

# Model 3
clog3ol <- polr(as.factor(salience_ches) ~ supporterdist:major
                + supporterdist
                + major:envt       
                + major:EU           
                + major:immig           
                + major:spendvtax  
                + major:redistribution          
                + major:ssm        
                + major:stint
                + major:civlib
                + country.issue,
                method="logistic",
                data=data,
                Hess=T)

coeftest(clog3ol, vcov=vcovCL(clog3ol, factor(dat$country.issue)))

nrow(model.frame(clog3ol)) 
logLik(clog3ol)

# Model 4
clog4ol <- polr(as.factor(salience_ches) ~ supporterdist:major
                + supporterdist
                + supportersd
                + govt_allyrs
                + partner_emph_ches
                + major:envt       
                + major:EU           
                + major:immig           
                + major:spendvtax  
                + major:redistribution          
                + major:ssm        
                + major:stint
                + major:civlib
                + country.issue ,
                method="logistic",
                data=data,
                Hess=T)

coeftest(clog4ol, vcov=vcovCL(clog4ol, factor(dat2$country.issue)))

nrow(model.frame(clog4ol)) 
logLik(clog4ol)


########################################################
# APPENDIX E: TABLE E.4                                #
########################################################

# Model 1 
cols1 <- felm(salience_ches ~ supporterext:major
              + supporterext
              + major:envt       
              + major:EU           
              + major:immig           
              + major:spendvtax  
              + major:redistribution          
              + major:ssm        
              + major:stint
              + major:civlib
              | country.issue | 0 | country.issue,
              exactDOF='rM',
              data=data)

summary(cols1)
nrow(cols1$fitted.values)

cols2 <- felm(salience_ches ~ supporterext:major
              + supporterext
              + supportersd
              + govt_allyrs
              + partner_emph_ches
              + major:envt       
              + major:EU           
              + major:immig           
              + major:spendvtax  
              + major:redistribution          
              + major:ssm        
              + major:stint
              + major:civlib
              | country.issue | 0 | country.issue,
              exactDOF='rM',
              data=data)

summary(cols2)
nrow(cols2$fitted.values)

cols3 <- felm(salience_ches ~ supporterdist:major
              + supporterdist
              + major:envt       
              + major:EU           
              + major:immig           
              + major:spendvtax  
              + major:redistribution          
              + major:ssm        
              + major:stint
              + major:civlib
              | country.issue | 0 | country.issue,
              exactDOF='rM',
              data=data)

summary(cols3)
nrow(cols3$fitted.values)

cols4 <- felm(salience_ches ~ supporterdist:major
              + supporterdist
              + supportersd
              + govt_allyrs
              + partner_emph_ches
              + major:envt       
              + major:EU           
              + major:immig           
              + major:spendvtax  
              + major:redistribution          
              + major:ssm        
              + major:stint
              + major:civlib
              | country.issue | 0 | country.issue,
              exactDOF='rM',
              data=data)

summary(cols4)
nrow(cols4$fitted.values)


########################################################
# APPENDIX E: TABLE E.5                                #
########################################################

# generating issue counter
bucpooled <- transform(bucpooled,issueno=as.numeric(factor(issue,exclude=NA)))

# writing function to drop issues one by one
drop_ci = function(data, i) {
  
  data.oe = subset(data,data$issueno!=i)
  
  data.oe <- as.data.frame(data.oe)
  
  data.oe$issue <- droplevels(as.factor(data.oe$issue))
  data.oe$country.issue <- droplevels(as.factor(data.oe$country.issue))
  
  coeff <- matrix(NA,nrow=1,ncol=4) # initializing local vector for coeffs & SEs
  
  results <- clogit(bucsal_ches ~ supporterext:major
                    + supporterext
                    + supportersd
                    + govt_allyrs
                    + partner_emph_ches
                    + major:issue
                    + strata(country.issue)
                    + cluster(country.issue),
                    data=data.oe,
                    method="efron")
  
  coeff[,1] = as.numeric(results$coefficients[1])  # extracting coefficients
  coeff[,2] = as.numeric(results$coefficients[5])  
  coeff[,3] = sqrt(results$var[1,1])               # extracting SEs
  coeff[,4] = sqrt(results$var[5,5])
  
  return(coeff)
  
}

# initializing global issue-level vector for coeffs & SEs
rob_coeffs <- matrix(NA,nrow=8,ncol=5)

# loop re-estimating model after dropping one issue at a time
for(i in 1:max(bucpooled$issueno)){
  
  print(i)
  
  rob_coeffs[i,5] <- unique(as.character(bucpooled$issue[bucpooled$issueno==i]))
  rob_coeffs[i,1:4] <- drop_ci(bucpooled,i)
  
}

# prints results, cols 1-2 = coeffs, cols 3-4 = respective SEs
rob_coeffs


########################################################
# APPENDIX E: TABLE E.6                                #
########################################################

# generating country counter
bucpooled <- transform(bucpooled,countryno=as.numeric(factor(country,exclude=NA)))

# writing function to drop countries one by one
drop_cib = function(data, i) {
  
  data.oe = subset(data,data$countryno!=i)
  
  data.oe <- as.data.frame(data.oe)
  
  data.oe$country <- droplevels(as.factor(data.oe$country))
  data.oe$country.issue <- droplevels(as.factor(data.oe$country.issue))

  coeff <- matrix(NA,nrow=1,ncol=4) # initializing local vector for coeffs & SEs
  
  results <- clogit(bucsal_ches ~ supporterext:major
                    + supporterext
                    + supportersd
                    + govt_allyrs
                    + partner_emph_ches
                    + major:envt       
                    + major:EU           
                    + major:immig           
                    + major:spendvtax  
                    + major:redistribution          
                    + major:ssm        
                    + major:stint
                    + major:civlib
                    + major:issue
                    + strata(country.issue)
                    + cluster(country.issue),
                    data=data.oe,
                    method="efron")
  
  coeff[,1] = as.numeric(results$coefficients[1])  
  coeff[,2] = as.numeric(results$coefficients[5])  
  coeff[,3] = sqrt(results$var[1,1])
  coeff[,4] = sqrt(results$var[5,5])
  
  return(coeff)
  
}

# initializing global country-level vector for coeffs & SEs
rob_coeffs2 <- matrix(nrow=max(bucpooled$countryno),ncol=ncol(rob_coeffs))

# loop re-estimating model after dropping one country at a time
for(i in 1:max(bucpooled$countryno)){
  
  print(i)
  
  rob_coeffs2[i,1:4] <- drop_cib(bucpooled,i)
  rob_coeffs2[i,5] <- unique(as.character(bucpooled$country[bucpooled$countryno==i]))
  
}

# prints results, cols 1-2 = coeffs, cols 3-4 = respective SEs
rob_coeffs2


########################################################
# APPENDIX E: TABLE E.7                                #
########################################################

# (1) > 10 Supporters

bucrob_supp1 <- subset(bucpooled,bucpooled$supporterno>10)  

csupp1 <- clogit(bucsal_ches ~ supporterext:major
                + supporterext
                + supportersd
                + govt_allyrs
                + partner_emph_ches
                + major:envt       
                + major:EU           
                + major:immig           
                + major:spendvtax  
                + major:redistribution          
                + major:ssm        
                + major:stint
                + major:civlib
                + strata(country.issue)
                + cluster(country.issue),
                data=bucrob_supp1,
                method="efron")

summary(csupp1)

nrow(model.frame(csupp1))/3 
logLik(csupp1)

# (2) > 15 Supporters

bucrob_supp2 <- subset(bucpooled,bucpooled$supporterno>15)  

csupp2 <- clogit(bucsal_ches ~ supporterext:major
                + supporterext
                + supportersd
                + govt_allyrs
                + partner_emph_ches
                + major:envt       
                + major:EU           
                + major:immig           
                + major:spendvtax  
                + major:redistribution          
                + major:ssm        
                + major:stint
                + major:civlib
                + strata(country.issue)
                + cluster(country.issue),
                data=bucrob_supp2,
                method="efron")

summary(csupp2)

nrow(model.frame(csupp2))/3 
logLik(csupp2)

# (3) Approach A

csupp3 <- clogit(bucsal_ches ~ supporterext_r1:major
                + supporterext_r1
                + supportersd_r1
                + govt_allyrs
                + partner_emph_ches
                + major:envt       
                + major:EU           
                + major:immig           
                + major:spendvtax  
                + major:redistribution          
                + major:ssm        
                + major:stint
                + major:civlib
                + strata(country.issue)
                + cluster(country.issue),
                data=bucpooled,
                method="efron")

summary(csupp3)

nrow(model.frame(csupp3))/3 
logLik(csupp3)

# (4) Approach B

csupp4 <- clogit(bucsal_ches ~ supporterext_r2:major
                + supporterext_r2
                + supportersd_r2
                + govt_allyrs
                + partner_emph_ches
                + major:envt       
                + major:EU           
                + major:immig           
                + major:spendvtax  
                + major:redistribution          
                + major:ssm        
                + major:stint
                + major:civlib
                + strata(country.issue)
                + cluster(country.issue),
                data=bucpooled,
                method="efron")

summary(csupp4)

nrow(model.frame(csupp4))/3 
logLik(csupp4)

# (5) Approach C

csupp5 <- clogit(bucsal_ches ~ supporterext_r3:major
                + supporterext_r3
                + supportersd_r3
                + govt_allyrs
                + partner_emph_ches
                + major:envt       
                + major:immig           
                + major:redistribution          
                + strata(country.issue)
                + cluster(country.issue),
                data=bucpooled,
                method="efron")

summary(csupp5)

nrow(model.frame(csupp5))/3 
logLik(csupp5)

# (6) Approach D

csupp6 <- clogit(bucsal_ches ~ supporterext_r4:major
                + supporterext_r4
                + supportersd_r4
                + govt_allyrs
                + partner_emph_ches
                + major:envt       
                + major:EU           
                + major:immig           
                + major:spendvtax  
                + major:redistribution          
                + major:ssm        
                + major:stint
                + major:civlib
                + strata(country.issue)
                + cluster(country.issue),
                data=bucpooled,
                method="efron")

summary(csupp6)

nrow(model.frame(csupp6))/3 
logLik(csupp6)


########################################################
# APPENDIX E: TABLE E.8                                #
########################################################

# each model uses a different timeframe to assign major/minor party status

## (1) 2014-2014 ##

bucpooled$maj1 <- ifelse(bucpooled$seats14>=13,1,0)

clogm1 <- clogit(bucsal_ches ~ supporterext:maj1
                 + supporterext
                 + supportersd
                 + govt_allyrs
                 + partner_emph_ches
                 + maj1:civlib
                 + maj1:EU
                 + maj1:immig
                 + maj1:spendvtax
                 + maj1:redistribution
                 + maj1:ssm
                 + maj1:stint
                 + maj1:envt
                 + strata(country.issue)
                 + cluster(country.issue),
                 data=bucpooled,
                 method="efron")

summary(clogm1)

nrow(model.frame(clogm1))/3 
logLik(clogm1)
AIC(clogm1)
BIC(clogm1)

## (2) 2010-2014 ##

bucpooled$maj2 <- ifelse(bucpooled$hst_avg10>=13,1,0)

clogm2 <- clogit(bucsal_ches ~ supporterext:maj2
                 + supporterext
                 + supportersd
                 + govt_allyrs
                 + partner_emph_ches
                 + maj2:civlib
                 + maj2:EU
                 + maj2:immig
                 + maj2:spendvtax
                 + maj2:redistribution
                 + maj2:ssm
                 + maj2:stint
                 + maj2:envt
                 + strata(country.issue)
                 + cluster(country.issue),
                 data=bucpooled,
                 method="efron")

summary(clogm2)

nrow(model.frame(clogm2))/3 
logLik(clogm2)
AIC(clogm2)
BIC(clogm2)

## (3) 2005-2014 ##

bucpooled$maj3 <- ifelse(bucpooled$hst_avg05>=13,1,0)

clogm3 <- clogit(bucsal_ches ~ supporterext:maj3
                 + supporterext
                 + supportersd
                 + govt_allyrs
                 + partner_emph_ches
                 + maj3:civlib
                 + maj3:EU
                 + maj3:immig
                 + maj3:spendvtax
                 + maj3:redistribution
                 + maj3:ssm
                 + maj3:stint
                 + maj3:envt
                 + strata(country.issue)
                 + cluster(country.issue),
                 data=bucpooled,
                 method="efron")

summary(clogm3)

nrow(model.frame(clogm3))/3 
logLik(clogm3)
AIC(clogm3)
BIC(clogm3)

## (4) 2000-2014 ##

bucpooled$majx <- ifelse(bucpooled$hst_avg00>=13,1,0)

clog2 <- clogit(bucsal_ches ~ supporterext:majx
                + supporterext
                + supportersd
                + govt_allyrs
                + partner_emph_ches
                + majx:civlib
                + majx:EU
                + majx:immig
                + majx:spendvtax
                + majx:redistribution
                + majx:ssm
                + majx:stint
                + majx:envt
                + strata(country.issue)
                + cluster(country.issue),
                data=bucpooled,
                method="efron")

summary(clog2)

nrow(model.frame(clog2))/3 
logLik(clog2)
AIC(clog2)
BIC(clog2)

## (5) 1995-2014 ##

bucpooled$maj4 <- ifelse(bucpooled$hst_avg95>=13,1,0)

clogm4 <- clogit(bucsal_ches ~ supporterext:maj4
                 + supporterext
                 + supportersd
                 + govt_allyrs
                 + partner_emph_ches
                 + maj4:civlib
                 + maj4:EU
                 + maj4:immig
                 + maj4:spendvtax
                 + maj4:redistribution
                 + maj4:ssm
                 + maj4:stint
                 + maj4:envt
                 + strata(country.issue)
                 + cluster(country.issue),
                 data=bucpooled,
                 method="efron")

summary(clogm4)

nrow(model.frame(clogm4))/3 
logLik(clogm4)
AIC(clogm4)
BIC(clogm4)

## (6) 1990-2014 ##

bucpooled$maj5 <- ifelse(bucpooled$hst_avg90>=13,1,0)

clogm5 <- clogit(bucsal_ches ~ supporterext:maj5
                 + supporterext
                 + supportersd
                 + govt_allyrs
                 + partner_emph_ches
                 + maj5:civlib
                 + maj5:EU
                 + maj5:immig
                 + maj5:spendvtax
                 + maj5:redistribution
                 + maj5:ssm
                 + maj5:stint
                 + maj5:envt
                 + strata(country.issue)
                 + cluster(country.issue),
                 data=bucpooled,
                 method="efron")

summary(clogm5)

nrow(model.frame(clogm5))/3 
logLik(clogm5)
AIC(clogm5)
BIC(clogm5)

## (7) 1980-2014 ##

bucpooled$maj6 <- ifelse(bucpooled$hst_avg80>=13,1,0)

clogm6 <- clogit(bucsal_ches ~ supporterext:maj6
                 + supporterext
                 + supportersd
                 + govt_allyrs
                 + partner_emph_ches
                 + maj6:civlib
                 + maj6:EU
                 + maj6:immig
                 + maj6:spendvtax
                 + maj6:redistribution
                 + maj6:ssm
                 + maj6:stint
                 + maj6:envt
                 + strata(country.issue)
                 + cluster(country.issue),
                 data=bucpooled,
                 method="efron")

summary(clogm6)

nrow(model.frame(clogm6))/3 
logLik(clogm6)
AIC(clogm6)
BIC(clogm6)

## (8) 1970-2014 ##

bucpooled$maj7 <- ifelse(bucpooled$hst_avg70>=13,1,0)

clogm7 <- clogit(bucsal_ches ~ supporterext:maj7
                 + supporterext
                 + supportersd
                 + govt_allyrs
                 + partner_emph_ches
                 + maj7:civlib
                 + maj7:EU
                 + maj7:immig
                 + maj7:spendvtax
                 + maj7:redistribution
                 + maj7:ssm
                 + maj7:stint
                 + maj7:envt
                 + strata(country.issue)
                 + cluster(country.issue),
                 data=bucpooled,
                 method="efron")

summary(clogm7)

nrow(model.frame(clogm7))/3 
logLik(clogm7)
AIC(clogm7)
BIC(clogm7)

## (9) 1960-2014 ##

bucpooled$maj8 <- ifelse(bucpooled$hst_avg60>=13,1,0)

clogm8 <- clogit(bucsal_ches ~ supporterext:maj8
                 + supporterext
                 + supportersd
                 + govt_allyrs
                 + partner_emph_ches
                 + maj8:civlib
                 + maj8:EU
                 + maj8:immig
                 + maj8:spendvtax
                 + maj8:redistribution
                 + maj8:ssm
                 + maj8:stint
                 + maj8:envt
                 + strata(country.issue)
                 + cluster(country.issue),
                 data=bucpooled,
                 method="efron")

summary(clogm8)

nrow(model.frame(clogm8))/3 
logLik(clogm8)
AIC(clogm8)
BIC(clogm8)

########################################################
# APPENDIX E: FIGURE E.1                               #
########################################################

Threshold <- c("2014-01-01","2010-01-01","2005-01-01","2000-01-01",
               "1995-01-01","1990-01-01","1980-01-01","1970-01-01",
               "1960-01-01") 

AICs <- c(AIC(clogm1),AIC(clogm2),AIC(clogm3),AIC(clog2),
          AIC(clogm4),AIC(clogm5),AIC(clogm6),AIC(clogm7),AIC(clogm8))

figE1 <- as.data.frame(cbind(AICs,Threshold))

figE1$Threshold <- as.Date.character(Threshold)
figE1$AICs <- as.numeric(as.character(figE1$AICs))

dev.new()

pdf(file="threshyrs.pdf", width=14, height=10)

plot(figE1$Threshold,figE1$AICs,
     ylim=c(2900,3100),
     type="b",
     lwd=3,
     col="red",
     xlab="Threshold Year for Historical Electoral Performance",
     ylab="AIC for Model of Major Party Emphasis Strategy")

numbers <- c(1:12,14:20) 

for(i in numbers){
  
  print(i)
  
  bucpooled$maj2 <- ifelse(bucpooled$hst_avg60>=i,1,0)
  
  clogv1 <- clogit(bucsal_ches ~ supporterext:maj2
                   + supporterext
                   + supportersd
                   + govt_allyrs
                   + partner_emph_ches
                   + maj2:civlib
                   + maj2:EU
                   + maj2:immig
                   + maj2:spendvtax
                   + maj2:redistribution
                   + maj2:ssm
                   + maj2:stint
                   + maj2:envt
                   + strata(country.issue)
                   + cluster(country.issue),
                   data=bucpooled,
                   method="efron")
  
  bucpooled$maj3 <- ifelse(bucpooled$hst_avg70>=i,1,0)
  
  clogv2 <- clogit(bucsal_ches ~ supporterext:maj3
                   + supporterext
                   + supportersd
                   + govt_allyrs
                   + partner_emph_ches
                   + maj3:civlib
                   + maj3:EU
                   + maj3:immig
                   + maj3:spendvtax
                   + maj3:redistribution
                   + maj3:ssm
                   + maj3:stint
                   + maj3:envt
                   + strata(country.issue)
                   + cluster(country.issue),
                   data=bucpooled,
                   method="efron")
  
  bucpooled$maj4 <- ifelse(bucpooled$hst_avg80>=i,1,0)
  
  clogv3 <- clogit(bucsal_ches ~ supporterext:maj4
                   + supporterext
                   + supportersd
                   + govt_allyrs
                   + partner_emph_ches
                   + maj4:civlib
                   + maj4:EU
                   + maj4:immig
                   + maj4:spendvtax
                   + maj4:redistribution
                   + maj4:ssm
                   + maj4:stint
                   + maj4:envt
                   + strata(country.issue)
                   + cluster(country.issue),
                   data=bucpooled,
                   method="efron")
  
  bucpooled$maj5 <- ifelse(bucpooled$hst_avg90>=i,1,0)
  
  clogv4 <- clogit(bucsal_ches ~ supporterext:maj5
                   + supporterext
                   + supportersd
                   + govt_allyrs
                   + partner_emph_ches
                   + maj5:civlib
                   + maj5:EU
                   + maj5:immig
                   + maj5:spendvtax
                   + maj5:redistribution
                   + maj5:ssm
                   + maj5:stint
                   + maj5:envt
                   + strata(country.issue)
                   + cluster(country.issue),
                   data=bucpooled,
                   method="efron")
  
  bucpooled$maj6 <- ifelse(bucpooled$hst_avg95>=i,1,0)
  
  clogv5 <- clogit(bucsal_ches ~ supporterext:maj6
                   + supporterext
                   + supportersd
                   + govt_allyrs
                   + partner_emph_ches
                   + maj6:civlib
                   + maj6:EU
                   + maj6:immig
                   + maj6:spendvtax
                   + maj6:redistribution
                   + maj6:ssm
                   + maj6:stint
                   + maj6:envt
                   + strata(country.issue)
                   + cluster(country.issue),
                   data=bucpooled,
                   method="efron")
  
  bucpooled$majx <- ifelse(bucpooled$hst_avg00>=i,1,0)
  
  clog2i <- clogit(bucsal_ches ~ supporterext:majx
                  + supporterext
                  + supportersd
                  + govt_allyrs
                  + partner_emph_ches
                  + majx:civlib
                  + majx:EU
                  + majx:immig
                  + majx:spendvtax
                  + majx:redistribution
                  + majx:ssm
                  + majx:stint
                  + majx:envt
                  + strata(country.issue)
                  + cluster(country.issue),
                  data=bucpooled,
                  method="efron")
  
  bucpooled$maj7 <- ifelse(bucpooled$hst_avg05>=i,1,0)
  
  clogv6 <- clogit(bucsal_ches ~ supporterext:maj7
                   + supporterext
                   + supportersd
                   + govt_allyrs
                   + partner_emph_ches
                   + maj7:civlib
                   + maj7:EU
                   + maj7:immig
                   + maj7:spendvtax
                   + maj7:redistribution
                   + maj7:ssm
                   + maj7:stint
                   + maj7:envt
                   + strata(country.issue)
                   + cluster(country.issue),
                   data=bucpooled,
                   method="efron")
  
  bucpooled$maj8 <- ifelse(bucpooled$hst_avg10>=i,1,0)
  
  clogv7 <- clogit(bucsal_ches ~ supporterext:maj8
                   + supporterext
                   + supportersd
                   + govt_allyrs
                   + partner_emph_ches
                   + maj8:civlib
                   + maj8:EU
                   + maj8:immig
                   + maj8:spendvtax
                   + maj8:redistribution
                   + maj8:ssm
                   + maj8:stint
                   + maj8:envt
                   + strata(country.issue)
                   + cluster(country.issue),
                   data=bucpooled,
                   method="efron")
  
  bucpooled$maj9 <- ifelse(bucpooled$seats14>=i,1,0)
  
  clogv8 <- clogit(bucsal_ches ~ supporterext:maj9
                   + supporterext
                   + supportersd
                   + govt_allyrs
                   + partner_emph_ches
                   + maj9:civlib
                   + maj9:EU
                   + maj9:immig
                   + maj9:spendvtax
                   + maj9:redistribution
                   + maj9:ssm
                   + maj9:stint
                   + maj9:envt
                   + strata(country.issue)
                   + cluster(country.issue),
                   data=bucpooled,
                   method="efron")
  
  AICs <- c(AIC(clogv8),AIC(clogv7),AIC(clogv6),AIC(clog2i),
            AIC(clogv5),AIC(clogv4),AIC(clogv3),AIC(clogv2),AIC(clogv1))
  
  figE1.1 <- as.data.frame(cbind(AICs,Threshold))
  
  figE1.1$Threshold <- as.Date.character(Threshold)
  figE1.1$AICs <- as.numeric(as.character(figE1.1$AICs))
  
  lines(figE1.1$Threshold,figE1.1$AICs,
        lty=2,
        lwd=2,
        col="orange")
  
}

dev.off()

########################################################
# DECLINING VS RISING MAJOR PARTIES                    #
########################################################

# coding party as old major if avg seat share btw 1990-2000 > 13%
bucpooled$oldmajor_st <- ifelse(bucpooled$oldmajor>=13,1,0)

# coding party as rising if gained seats in last election
bucpooled$rising <- ifelse(bucpooled$hst_t0>bucpooled$hst_t1,1,0)

# Model 1
clog_g1 <- clogit(bucsal_ches ~ supporterext:oldmajor_st
                  + supporterext:major
                  + supporterext
                  + oldmajor_st
                  + major:envt       
                  + major:EU           
                  + major:immig           
                  + major:spendvtax  
                  + major:redistribution          
                  + major:ssm        
                  + major:stint
                  + major:civlib
                  + strata(country.issue)
                  + cluster(country.issue),
                  data=bucpooled,
                  method="efron")

summary(clog_g1)
nrow(model.frame(clog_g1))/3  
logLik(clog_g1) 

# Model 2
clog_g2 <- clogit(bucsal_ches ~ supporterext:oldmajor_st
                  + supporterext:major
                  + supporterext
                  + oldmajor_st
                  + supportersd
                  + govt_allyrs
                  + partner_emph_ches
                  + major:envt       
                  + major:EU           
                  + major:immig           
                  + major:spendvtax  
                  + major:redistribution          
                  + major:ssm        
                  + major:stint
                  + major:civlib
                  + strata(country.issue)
                  + cluster(country.issue),
                  data=bucpooled,
                  method="efron")

summary(clog_g2)
nrow(model.frame(clog_g2))/3  
logLik(clog_g2) 

# Model 3
clog_g3 <- clogit(bucsal_ches ~ supporterext:rising:major
                  + supporterext:major
                  + supporterext:rising
                  + supporterext
                  + rising
                  + major:envt       
                  + major:EU           
                  + major:immig           
                  + major:spendvtax  
                  + major:redistribution          
                  + major:ssm        
                  + major:stint
                  + major:civlib
                  + strata(country.issue)
                  + cluster(country.issue),
                  data=bucpooled,
                  method="efron")

summary(clog_g3)
nrow(model.frame(clog_g3))/3  
logLik(clog_g3) 

# Model 4
clog_g4 <- clogit(bucsal_ches ~ supporterext:rising:major
                  + supporterext:major
                  + supporterext:rising
                  + supporterext
                  + rising
                  + supportersd
                  + govt_allyrs
                  + partner_emph_ches
                  + major:envt       
                  + major:EU           
                  + major:immig           
                  + major:spendvtax  
                  + major:redistribution          
                  + major:ssm        
                  + major:stint
                  + major:civlib
                  + strata(country.issue)
                  + cluster(country.issue),
                  data=bucpooled,
                  method="efron")

summary(clog_g4)
nrow(model.frame(clog_g4))/3  
logLik(clog_g4) 

sink()